package com.drow.aips;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.drow.aips.util.DialogUtil;
import com.drow.aips.util.HttpUtil;

/**
 * 
 * 类名称：Notifications.java
 * <P/>
 * 类描述： 发送通知类
 * <P/>
 * 创建时间：2012-12-24
 * <P/>
 * 创建人： 史守阳
 * <P/>
 * 联系方式： ssy.vip^qq.com
 * <P/>
 * 修改人：无
 * <P/>
 * 修改时间：无
 * <P/>
 * 修改备注：无
 * <P/>
 * 版本：v1.0
 *
 */
public class Notifications extends Activity {

	// 定义界面中三个文本框
	EditText Etitle, EcalledNumber,Econtent;
	// 定义界面中三个按钮
	Button Bcontacts,Bsend, Bcancel;
	
	TextView TtextView;
	
	CheckBox Cvoice,Csms;
	
	String aIds = null;
	Boolean flagVoice = true,flagSMS = true;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notifications);
		
		Etitle = (EditText) findViewById(R.id.title);
		EcalledNumber = (EditText) findViewById(R.id.callednumber);
		Econtent = (EditText) findViewById(R.id.content);
		
		TtextView = (TextView) findViewById(R.id.textViewContacts);
		Bcontacts = (Button) findViewById(R.id.notifications_contacts);
		Bsend = (Button) findViewById(R.id.send);
		Bcancel = (Button) findViewById(R.id.cancel);
		Cvoice = (CheckBox) findViewById(R.id.notifications_voice);
		Cvoice.setChecked(true);
		Csms = (CheckBox) findViewById(R.id.notifications_sms);
		Csms.setChecked(true);
		
		Cvoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
				{
					flagVoice = true;
				}else
				{
					flagVoice = false;
				}
			}
		});
		
		Csms.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
				{
					flagSMS = true;
				}else
				{
					flagSMS = false;
				}
			}
		});
		
		Bcontacts.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//打开新的Activity
		    	Intent intent = new Intent(Notifications.this, Contacts.class);
		    	Bundle bundle = new Bundle();
		    	bundle.putString("", "");
		    	intent.putExtras(bundle);
		    	
		    	//startActivity(intent);
		    	startActivityForResult(intent, 100);
			}
		});
		
		Bcancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// 启动Main Activity
				Intent intent = new Intent(Notifications.this, Home.class);
				startActivity(intent);
				// 结束该Activity
				finish();
			}
		});
		Bsend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				String title = Etitle.getText().toString();
				String calledNumber = EcalledNumber.getText().toString();
				String content = Econtent.getText().toString();
				String type = flagSMS==true?"1":"" + (flagVoice==true?",3":"");
				
				boolean flag = verification(title, calledNumber, content, type);
				
				try 
				{
					if(flag)
					{
						JSONObject jsonObj = notifications(title,type,aIds,calledNumber,content);
						
						Toast.makeText(getApplicationContext(), jsonObj.getString("msg"),  Toast.LENGTH_LONG).show();
					}
						
				} catch (Exception e) {

					DialogUtil.showDialog(Notifications.this, "通知发送失败！", false);
					
					e.printStackTrace();
				}
			}
		});
		
	}
	
	// 定义发送请求的方法
	private JSONObject notifications(String title,String type,String aIds,String calledNumber, String content) throws Exception
	{
		// 使用Map封装请求参数
		Map<String, String> map = new HashMap<String, String>();
		map.put("title", title);
		map.put("type", type);
		map.put("calledNumber", calledNumber.toString());
		map.put("aIds", aIds);
		map.put("content", content);
		// 定义发送请求的URL
		String url = HttpUtil.BASE_URL + "notifications.jsp";
		// 发送请求
		return new JSONObject(HttpUtil.postRequest(url, map));
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		aIds = data.getStringExtra("aIds");
		
		if(null!=aIds&&aIds.split(",").length>0){
			TtextView.setText("已选择： "+aIds.split(",").length+" 人");
			Toast.makeText(getApplicationContext(),"读取通讯录成功\n\n您一共选择了"+aIds.split(",").length+"位员工", Toast.LENGTH_LONG).show();
		}else{
			TtextView.setText("已选择： 0 人");
			Toast.makeText(getApplicationContext(),"您未选择员工\n\n请重新选择通讯录\n\n或\n\n请手动输入联系人电话", Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_LONG).show();
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
		{
			// 启动Main Activity
			Intent intent = new Intent(Notifications.this, Home.class);
			startActivity(intent);
			// 结束该Activity
			finish();
		}else if(keyCode == KeyEvent.KEYCODE_MENU) 
		{
			Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有",  Toast.LENGTH_LONG).show();
		}
		return true;
	}

	public Boolean verification(String title, String calledNumber, String content, String type) {
		
		if("".equals(title)){
			Toast.makeText(getApplicationContext(), "请输入通知的标题",  Toast.LENGTH_LONG).show();
			Etitle.requestFocus();
			
			return false;
		}
		
		if("".equals(calledNumber)&&("".equals(aIds)||null==aIds))
		{
			Toast.makeText(getApplicationContext(), "请从通讯录选择接收人\n或\n输入系统外的接收号码",  Toast.LENGTH_LONG).show();
			EcalledNumber.requestFocus();
			
			return false;
		}
		
		if("".equals(content))
		{
			Toast.makeText(getApplicationContext(), "请输入通知内容",  Toast.LENGTH_LONG).show();
			Econtent.requestFocus();
			
			return false;
		}
		
		if("".equals(type)){
			Toast.makeText(getApplicationContext(), "请选择发送通知的类型",  Toast.LENGTH_LONG).show();
			
			return false;
		}
		
		return true;
	}

}
